<?php if(!defined('OCTOMS')){header('HTTP/1.1 403');die('{"error":"forbidden"}');}
/**
 * @package       OctoMS.com
 * @subpackage    DesignJotter
 * @copyright     Copyright 2011, Valentino-Jivko Radosavlevici (http://valentino.radosavlevici.com)
 * @license       GPL v3.0 (http://www.gnu.org/licenses/gpl-3.0.txt)
 * 
 * Redistributions of files must retain the above copyright notice.
 * 
 * @since         OctoMS 0.0.1
 */
	
	/**
	 * Session management; 0731 458 795
	 * 
	 * 
	 * 
	 * @package OctoMS
	 * @subpackage session
	 * @version 0.1
	 * 
	 * @author Valentino-Jivko Radosavlevici
	 */
	class session_cl
	{
		
		/**
		 * Check if the session was started
		 * 
		 * @example $this->session->is_started();
		 * 
		 * @return boolean - True if the session was started already
		 * @package OctoMS.com
		 * 
		 * @author Valentino-Jivko Radosavlevici
		 */
		function is_started()
		{
			return session_id() != '';	
		}// end function is_started()
		
		/**
		 * Start the session; prevent cookie stealing and session sniffing
		 * 
		 * @example $this->session->start();
		 * 
		 * 
		 * @return session_cl
		 * 
		 * @author Valentino-Jivko Radosavlevici
		 */
		function start()
		{	
			if (!$this->is_started())
			{
				session_start();
			}
			
			return $this;
			
		}// end function start()
		
		/**
		 * Destroy the current session
		 * 
		 * @example $this->session->destroy();
		 * 
		 * 
		 * @return session_cl
		 * 
		 * @author Valentino-Jivko Radosavlevici
		 */
		function destroy()
		{
			// Clear all session data
			unset($_SESSION);
			
		}// end function destroy()
		
		/**
		 * Set a session value
		 * 
		 * @example 
		 * // Set key=>value
		 * $this->session->set('key','value');
		 * // Set k1=>v1, k2=>v2
		 * $this->session->set(array('k1'=>'v1','k2'=>'v2'));
		 * 
		 * 
		 * 
		 * @param $key and $value or $array
		 * @return session_cl
		 * 
		 * @author Valentino-Jivko Radosavlevici
		 */
		function set()
		{
			$params = func_get_args();
			if (isset($params[0]) && is_array($params[0]))
			{
				foreach ($params AS $k=>$v)
				{
					$_SESSION[$k] = $v;
				}
			}
			elseif (isset($params[0]) && isset($params[1]))
			{
				$_SESSION[$params[0]] = $params[1];
			}
			
			return $this;
		}// end function set()
		
		/**
		 * Get a session value by key
		 * 
		 * @example $this->session->get('key');
		 * 	$this->session->get(array('key1','key2'));
		 * 
		 * 
		 * @param string/array $key(s)
		 * @return mixed
		 * 
		 * @author Valentino-Jivko Radosavlevici
		 */
		function get($keys=NULL)
		{
			if (is_array($keys))
			{
				$result = array();
				foreach ($keys AS $k)
				{
					$result[$k] = isset($_SESSION[$k])?$_SESSION[$k]:NULL;
				}
				return $result;
			}
			elseif (is_string($keys))
			{
				return isset($_SESSION[$keys])?$_SESSION[$keys]:NULL;
			}
			
			return NULL;
		}// end function get()
		
		/**
		 * Unset a sessions value by key
		 * 
		 * @example $this->session->reset('key');
		 * 	$this->session->reset(array('key1','key2'));
		 * 	$this->session->reset(); # clean all session values
		 * 
		 * 
		 * @param string/array $key(s)
		 * @return session_cl
		 * 
		 * @author Valentino-Jivko Radosavlevici
		 */
		function reset($keys=NULL)
		{
			if (is_array($keys))
			{
				foreach($keys AS $k)
				{
					unset($_SESSION[$k]);
				}
			}
			elseif (is_string($keys))
			{
				unset($_SESSION[$keys]);
			}
			else
			{
				unset($_SESSION);
			}
			
			return $this;
			
		}// end function reset()
		
		/**
		 * Set a flash variable or a set of flash variables
		 * 
		 * @example $this->session->flash_set('key','value');
		 * 	$this->session->flash_set(array('val1','val2'));
		 * 
		 * 
		 * @param $key and $value or $array
		 * @return session_cl 
		 * 
		 * @author Valentino-Jivko Radosavlevici
		 */
		function flash_set()
		{
			$params = func_get_args();
			if (isset($params[0]) && is_array($params[0]))
			{
				foreach ($params[0] AS $k => $v)
				{
					$_SESSION['__flash__'][$k] = $v;
				}
			}
			elseif (isset($params[0]) && isset($params[1]))
			{
				$_SESSION['__flash__'][$params[0]] = $params[1];
			}
			
			return $this;
		}// end function flash_set()
		
		/**
		 * Get a flash value (and, obviously, unset it)
		 * 
		 * @example $this->session->flash_get('key');
		 * 	$this->session->flash_get(array('key1','key2'));
		 * 
		 * 
		 * @param string/array $key(s)
		 * @return mixed
		 * 
		 * @author Valentino-Jivko Radosavlevici
		 */
		function flash_get($keys=NULL)
		{
			if (is_array($keys))
			{
				$result = array();
				foreach ($keys AS $k)
				{
					$result[$k] = isset($_SESSION['__flash__'][$k])?$_SESSION['__flash__'][$k]:NULL;
					unset($_SESSION['__flash__'][$k]);
				}
				return $result;
			}
			elseif (is_string($keys))
			{
				if (isset($_SESSION['__flash__'][$keys])){
					$temp = $_SESSION['__flash__'][$keys];
					unset($_SESSION['__flash__'][$keys]);
					return $temp;
				}
				else 
				{
					return NULL;
				}
			}
			
			return NULL;
			
		}// end function flash_get()
		
		/**
		 * Flash reset
		 * 
		 * @example $this->session->flash_reset('key');
		 * 	$this->session->flash_reset(array('key1','key2'));
		 * 	$this->session->flash_reset(); # unset all flash data
		 * 
		 * 
		 * @param string/array $keys
		 * @return session_cl
		 * 
		 * @author Valentino-Jivko Radosavlevici
		 */
		function flash_reset($keys=NULL)
		{
			if (!empty($keys))
			{
				$this->flash_get($keys);
			}
			else 
			{
				unset($_SESSION['__flash__']);
			}
			
			return $this;
			
		}// end function flash_reset()
		
	}// end class session_cl

	
/* End Of File <session.inc> */